#include <QApplication>
#include <QPlainTextEdit>
#include <QSyntaxHighlighter>
#include <QTextCharFormat>
#include <QRegularExpression>

class CodeHighlighter : public QSyntaxHighlighter
{
public:
    explicit CodeHighlighter(QTextDocument *parent = nullptr) : QSyntaxHighlighter(parent)
    {
        setHghtLight(parent);
    }

    void setHghtLight(QTextDocument* textDocument = nullptr) {
        QTextCharFormat keywordFormat;
        keywordFormat.setForeground(Qt::blue);
        QStringList keywords = {
            "if", "else", "for", "while", "int", "double", "return"
            // 添加其他关键字...
        };
            foreach (const QString &pattern, keywords) {
                rule.pattern = QRegularExpression("\\b" + pattern + "\\b");
                rule.format = keywordFormat;
                highlightingRules.append(rule);
            }

        QTextCharFormat singleLineCommentFormat;
        singleLineCommentFormat.setForeground(Qt::darkGreen);
        rule.pattern = QRegularExpression("//[^\n]*");
        rule.format = singleLineCommentFormat;
        highlightingRules.append(rule);


        multiLineCommentFormat.setForeground(Qt::darkGreen);
        commentStartExpression = QRegularExpression("/\\*");
        commentEndExpression = QRegularExpression("\\*/");
    }
protected:
    void highlightBlock(const QString &text) override
    {
        for (const HighlightingRule &rule : highlightingRules) {
            QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
            while (matchIterator.hasNext()) {
                QRegularExpressionMatch match = matchIterator.next();
                setFormat(match.capturedStart(), match.capturedLength(), rule.format);
            }
        }

        setCurrentBlockState(0);

        int startIndex = 0;
        if (previousBlockState() != 1)
            startIndex = text.indexOf(commentStartExpression);

        while (startIndex >= 0) {
            QRegularExpressionMatch match = commentEndExpression.match(text, startIndex);
            int endIndex = match.capturedStart();
            int commentLength = endIndex - startIndex + match.capturedLength();
            setFormat(startIndex, commentLength, multiLineCommentFormat);
            startIndex = text.indexOf(commentStartExpression, startIndex + commentLength);
        }
    }

private:
    typedef struct HighlightingRule
    {
        QRegularExpression pattern;
        QTextCharFormat format;
    } S_HighlightingRule;

    S_HighlightingRule rule;

    QVector<HighlightingRule> highlightingRules;
    QTextCharFormat multiLineCommentFormat;
    QRegularExpression commentStartExpression;
    QRegularExpression commentEndExpression;
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QPlainTextEdit textEdit;
    CodeHighlighter highlighter(textEdit.document());
    textEdit.setPlainText("int main() {\n    // This is a single-line comment\n    /* This is a multi-line comment */\n    return 0;\n}");

    textEdit.show();
    return a.exec();
}
